package com.example.administrator.androidhomework2;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private static  final String TAG = "MainActivity";

    TextView mTvResult;
    Button mBtnprogress;
    ProgressDialog progressDialog;
    Button mBtnMyDialog;

    //显示对话框
    private  void  showProgressDialog() {
        progressDialog = new ProgressDialog(this);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progressDialog.show();
    }
    //关闭对话框
    private  void closeProgressDialog(){
        progressDialog.dismiss();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.activity_main);

        mBtnMyDialog = findViewById(R.id.btn_mydialog);
        mBtnMyDialog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //调用显示自定义对话框
                MyDialog myDialog = new MyDialog(MainActivity.this);
                myDialog.show();
            }
        });


        mBtnprogress = findViewById(R.id.btn_progress);
        mBtnprogress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //显示 进度对话框
                showProgressDialog();
                Thread thread = new Thread(new Runnable() {
                    @Override
                    public void run() {
                        //等5秒钟，关闭进度对话框
                        try{
                            Thread.sleep(5000);//休眠5秒，模拟下载的过程
                            closeProgressDialog();//关闭进度对话框
                        } catch (InterruptedException e){
                            e.printStackTrace();
                        }
                    }
                });
                //启动线程
                thread.start();
            }
        });




        mTvResult = findViewById(R.id.tv_result);
        mTvResult.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog dialog = new AlertDialog.Builder(MainActivity.this)
                        .setTitle("请选择性别")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        })
                        .setNegativeButton("取消",null)
                        .setIcon(R.mipmap.ic_launcher)
                        .setMultiChoiceItems(new String[]{"辣","粉","牛肉"},null,null)
                        .setSingleChoiceItems(new String[]{"男", "女"}, -1, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Log.e(TAG,"onClick" +which);
                                //将用户的选择显示到Activity中
                                //获取显示数据的控件
                                TextView tvResult = findViewById(R.id.tv_result);
                                //根据选项数据，显示内容
                                tvResult.setText(which==0?"男":"女");
                            }
                        })
                        .create();
                dialog.show();
            }
        });





    }
    public  void  setmTvResult(String name){
        mTvResult.setText(name);
    }
}
